<?php

namespace app\common\model\mysql;

use think\Model;

class SysUserPost extends Model
{
    /**
     * 根据$ids获取分配岗位数据
     * @param $ids
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function passPostDelIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['post_id','in',$ids]
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    //批量插入岗位与用户关联表数据
    public function insertAllPostUser($userId,$postIds){
        $data = [];
        foreach ($postIds as $postId) {
            $data[] = [
                'post_id' => $postId,
                'user_id' => $userId,
            ];
        }
        return $this->insertAll($data);
    }

    /**
     * 根据用户id查找符合要求岗位数据
     * @param $ids
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPostIds($user_id){
        if(empty($user_id)){
            return false;
        }
        $where = [
            ['user_id','=',$user_id]
        ];
        $result = $this->where($where)->column('post_id');
        return $result;
    }

    //删除用户与岗位关联表数据
    public function deleteAllUserPost($user_id){
        if(empty($user_id)){
            return false;
        }
        $where = [
            "user_id" =>$user_id,
        ];
        return $this->where($where)->delete();
    }

    //删除用户与岗位关联表数据
    public function deleteAllInUserPost($user_id){
        if(empty($user_id)){
            return false;
        }
        $user_id = explode(',', $user_id);
        $where = [
            ["user_id",'in',$user_id],
        ];
        return $this->where($where)->delete();
    }
}
